package thinkingInJava.io;
// page 563 内存映射修改大文件中的一部分
//: io/LargeMappedFiles.java
//Creating a very large file using mapping.
//{RunByHand}
import java.nio.*;
import java.nio.channels.*;
import java.io.*;
import static thinkingInJava.YTool.YPrint.*;

public class LargeMappedFiles {
	static int length = 0x8FFFFFF; // 128 MB
	public static void main(String[] args) throws Exception {
		MappedByteBuffer out =
				new RandomAccessFile("thinkingInJava.dat", "rw").getChannel()
				.map(FileChannel.MapMode.READ_WRITE, 0, length); // 可以指定映射文件的哪一段
		for(int i = 0; i < length; i++)
			out.put((byte)'x');
		print("Finished writing");
		for(int i = length/2; i < length/2 + 6; i++)
			println((char)out.get(i));
	}
} ///:~
